import Taro, {Component, Config} from '@tarojs/taro'
import {View,} from '@tarojs/components'
import common from "../../common/js/common";


type Props = {
  userInfo: any;
}

type PageState = {
  channel: string;
}

export default class CommonInSite extends Component<Props, PageState> {
  config: Config = {
    navigationBarTitleText: '页面加载中……'
  };

  userInfo: any;

  constructor(props) {
    super(props);
    this.state = {
      channel: '普通',
    }
  }

  getQueryStringArgs(qs) {
    var args = {};
    var items = qs.length > 0 ? qs.split('&') : [];
    var item = [];
    var name = '';
    var value = '';
    for (var i = 0; i < items.length; i++) {
      item = items[i].split("=");
      //用decodeURIComponent()分别解码name 和value（因为查询字符串应该是被编码过的）。
      name = decodeURIComponent(item[0]);
      value = decodeURIComponent(item[1]);
      if (name.length) {
        args[name] = value
      }
    }
    return args
  }


  getUrlParams = () => {
    let query = this.$router.params;
    let scene = query.scene;
    if (scene) {
      let sceneStr = decodeURIComponent(scene);
      query = this.getQueryStringArgs(sceneStr)
    }
    return query
  };

  componentWillMount() {
    let that = this;
    console.log("componentWillMount===>")
    that.initUserInfo();

    Taro.showLoading({title: '加载中…', mask: true});
    let query = that.getUrlParams()
    let backUrl = query.backUrl;
    console.log("backUrl===>",backUrl)
    if (backUrl) {
      backUrl = decodeURIComponent(backUrl);
      Taro.reLaunch({url: backUrl}).then()
    }else{
      Taro.switchTab({url: '/pages/index/index'})
    }
  }

  async initUserInfo() {
    let userInfo = common.getUserInfo();
    // Taro.getUserProfile();
    if (!userInfo) {
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                console.log("res.userInfo===>",res, res.userInfo,)
                common.setUserInfo(res.userInfo);
            }
          })
    } else {
        if(!userInfo.openId){
           let openId = await common.getUserOpenId();
           userInfo.openId = openId;
        }
        await common.authLogin(userInfo);
    }
    common.getUserLevel().then(() => {
        
    })
}

  render() {
    return (
      <View>


      </View>
    )
  }
}

